def bubble_sort(nums: list[int]):
    """冒泡排序"""  # 升序：把最大值“冒泡”到右边
  
    n = len(nums)  
    for i in range(n - 1, 0, -1):  # 外循环：右 [n-1, 1] 左
        flag = False  # 若内层循环未发生交换说明已经排好
        for j in range(i):  # 内循环：左 [0, i-1] 右
            if nums[j] > nums[j + 1]:
                nums[j], nums[j + 1] = nums[j + 1], nums[j]
                flag = True
        if not flag:
            break


if __name__ == "__main__":
    nums = [4, 1, 3, 1, 5, 2]
    bubble_sort(nums)
    print("排序完成后 nums =", nums)  
    # 排序完成后 nums = [1, 1, 2, 3, 4, 5]
